package com.junzhiit.nengji.dao.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.bainaeco.framework.base.dao.AbstractBaseDAO;
import com.bainaeco.framework.common.jdbc.StatementParameter;
import com.bainaeco.framework.enums.IsDelete;
import com.junzhiit.nengji.bo.FavoriteShopBO;
import com.junzhiit.nengji.dao.FavoriteShopDAO;
import com.junzhiit.nengji.model.FavoriteShop;

@Repository
public class FavoriteShopDAOImpl extends AbstractBaseDAO<FavoriteShop> implements FavoriteShopDAO {
	
	@Override
	public FavoriteShop getByCondition(String userId, String shopId) {
		String sql="select * from favorite_shop where is_delete = ? and user_id = ? and shop_id = ?";
		StatementParameter params = new StatementParameter();
		params.setInt(IsDelete.NO.toIntValue());
		params.setString(userId);
		params.setString(shopId);
		return query(sql, FavoriteShop.class, params);
	}

	@Override
	public Integer countShopCollections(String shopId) {
		String sql="select count(*) from favorite_shop where is_delete = ? and shop_id = ?";
		
		StatementParameter params = new StatementParameter();
		params.setInt(IsDelete.NO.toIntValue());
		params.setString(shopId);
		
		return queryForInt(sql, params);
	}

	@Override
	public FavoriteShop getFavoriteShop(String userId,String shopId) {
		StatementParameter params = new StatementParameter();
		StringBuilder sql = new StringBuilder();
		sql.append("select * from favorite_shop where user_id = ? and shop_id = ? and is_delete =?");
		params.setString(userId);
		params.setString(shopId);
		params.setInt(IsDelete.NO.toIntValue());
		return super.query(sql.toString(), FavoriteShop.class, params);
	}

	@Override
	public List<FavoriteShopBO> getFavoriteShopList(Integer pageNumber,Integer maxPageRow,String userId,String shopId) {
		StringBuilder sql = new StringBuilder();
		StatementParameter params = new StatementParameter();
		sql.append("select s.*,fs.id as favoriteShopId,fs.user_id as userId,fs.shop_id as shopId");
		sql.append(" from favorite_shop fs");
		sql.append(" inner join shop s on s.id = fs.shop_id");
		sql.append(" where fs.user_id =? and fs.is_delete =? ");
		params.setString(userId);
		params.setInt(IsDelete.NO.toIntValue());
		
		if(StringUtils.isNotBlank(shopId)){
			sql.append(" and fs.shop_id = ? ");
			params.setString(shopId);
		}
		
		return super.queryForPagingList(sql.toString(),FavoriteShopBO.class, params, pageNumber, maxPageRow);
	}

	@Override
	public int getFavoriteShopTotalCount(String userId) {
		StringBuilder sql = new StringBuilder();
		StatementParameter params = new StatementParameter();
		sql.append("select count(fs.id) from favorite_shop fs ");
		sql.append(" inner join shop s on s.id = fs.shop_id");
		sql.append(" where fs.user_id =? and fs.is_delete =?");
		params.setString(userId);
		params.setInt(IsDelete.NO.toIntValue());
		return super.queryForInt(sql.toString(), params);
	}
	
	@Override
	public int favoriteShopNumber(String userId) {
		String sql = "select count(id) from favorite_shop where user_id =? and is_delete =?";
		StatementParameter params = new StatementParameter();
		params.setString(userId);
		params.setInt(IsDelete.NO.toIntValue());
		return super.queryForInt(sql,params);
	}
	
}
